/**
 * Copyright (c) 2014-2018, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.work.test;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Xue Xue (xuexue1@huli.com)
 * @version $Id$
 * @since 2020/3/31
 */
public class MySqlRegexPatternMain {
    private static final String filePath = "/Users/xuexue/Downloads/上线SQL/riskmanage_riskparam_表单.sql";

    public static void main(String[] args) throws IOException {
//        try {
        FileReader reader = new FileReader(MySqlRegexPatternMain.filePath);
        BufferedReader br = new BufferedReader(reader);
        String str = null;
        String nameRegex = "(\\d+)(,\')(.*)(\')";
        Pattern namePattern = Pattern.compile(nameRegex, Pattern.CASE_INSENSITIVE);

        Set<String> modelSet = new HashSet<>();
        HashMap<String, List<String>> paramsConfig = new HashMap<>();


        while ((str = br.readLine()) != null) {
            if (!str.startsWith("INSERT INTO")) {
                continue;
            }

            Matcher m = namePattern.matcher(str);
            while (m.find()) {
                String value = m.group(3);
                value = value.replace("'", "");
                String[] parts = value.split(",");
                //                    System.out.println("参数名称 = " + parts[0]);
                String paramName = parts[0]+"("+parts[1]+")";
                String config = parts[2];
                String[] arr1 = config.split(",");
                for (String str1 : arr1) {
                    String[] pair = str1.split("=");
                    String keyP = pair[0];
                    String valueP ="";
                    if(pair.length>1) {
                         valueP = pair[1];
                    }else{
                        System.out.println(paramName+"====Exception");
                    }
                    if ("model".equalsIgnoreCase(keyP)) {
                        modelSet.add(valueP);
                        if (paramsConfig.get(valueP) == null) {
                            paramsConfig.put(valueP, new ArrayList<String>() {{
                                add(paramName);
                            }});
                        } else {
                            List<String> params = paramsConfig.get(valueP);
                            params.add(paramName);
                            paramsConfig.put(valueP, params);
                        }
                    }

                }

            }
        }
        System.out.println(JSON.toJSONString(paramsConfig,true));
        br.close();
        reader.close();
//        } catch (Exception e) {
//            System.out.println(e.);
//        }
    }
}